﻿using AutoMapper;
using MediatR;
using SupplyChain.Domain.Entities;
using SupplyChain.Infrastructure.Interface;
using SupplyChain.Write.API.Application.Command.PurchaseOrder;
using SupplyChain.Write.API.Application.CommandHandler.Supplier;

namespace SupplyChain.Write.API.Application.CommandHandler.PurchaseOrder
{
    public class AddPurchaseCategoryHandler : IRequestHandler<AddPurchaseCategoryCommand, bool>
    {
        private readonly IBaseRepository<PurchaseCategory> repository;
        private readonly IMapper mapper;

        public AddPurchaseCategoryHandler(IBaseRepository<PurchaseCategory> repository, IMapper mapper)
        {
            this.repository = repository;
            this.mapper = mapper;
        }

        public async Task<bool> Handle(AddPurchaseCategoryCommand request, CancellationToken cancellationToken)
        {
            PurchaseCategory category = mapper.Map<PurchaseCategory>(request);
            bool result = await repository.AddAsync(category);
            return await Task.FromResult(result);
        }
    }
}
